import cv2
import numpy as np

# 摄像头
"""
# 启动摄像头(参数为0)，播放视频（参数为视频地址）
cap = cv2.VideoCapture(0)
# 设置Windows宽
cap.set(3, 640)
# 设置Windows高
cap.set(4, 480)
# 设置亮度
cap.set(10,300)
while True:
    # 监听每一帧的图像
    success, img = cap.read()
    # 展示每一帧的图像
    cv2.imshow("22",img)
    # 获取“q”来关闭
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break
"""

# 图像
"""
# 创建图像(参数地址)
img = cv2.imread("5.png")
# 设置哪一种方式来处理图像
imgGray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# 模糊化(将n*n像素大小视为一个像素大小,n必须是奇数)
imgBlur = cv2.GaussianBlur(imgGray,(11,11),0)
# 图像边缘提取(低于参数1的像素点被认为不是边缘，高于参数2的像素点被认为是边缘)
imgCanny = cv2.Canny(img,50,200)
# 扩大边缘像素大小（膨胀）
kernel = np.ones((3,3),np.uint8)    # 设置扩大后的像素大小
imgDilation = cv2.dilate(imgCanny,kernel,iterations=1) # iterations表示扩大的量,迭代次数
# 缩小边缘像素大小（腐蚀）
imgEroded = cv2.erode(imgDilation,kernel,iterations=2)
cv2.imshow("img",img)
cv2.imshow("imgGray",imgGray)
cv2.imshow("imgCanny",imgCanny)
cv2.imshow("imgDilation",imgDilation)
cv2.imshow("imgEroded",imgEroded)
cv2.waitKey(0)
"""

# 调整大小
"""
img = cv2.imread("5.png")
imgResize = cv2.resize(img,(300,300))
cv2.imshow("2",imgResize)
cv2.waitKey()
"""

# 画图像
"""
# 设置为0的像素点（即黑色的像素点）（参数一是图像大小，参数二是）
img = np.zeros((512,512,3))
# 设置色彩的区域（区间--颜色（rgb））
# img[200:300,100:200] = 255,255,0

# 画线（起始位置，终止位置，颜色，厚度）
cv2.line(img,(0,0),(200,200),(0,255,255))

# 画方框（起始位置，终止位置，颜色，厚度）   厚度可以通过cv2.FILLED进行完全填充
cv2.rectangle(img,(0,0),(250,300),(0,0,255),3)

# 画圆（起始位置，终止位置，颜色，厚度）   厚度可以通过cv2.FILLED进行完全填充
cv2.circle(img,(100,100),50,(0,0,255),3,)

# 写内容（文本，起始位置，字体，大小，颜色，厚度）
cv2.putText(img,'HELLO WORLD',(200,200),cv2.FONT_ITALIC,.7,(255,0,0),2)
cv2.imshow("22",img)
cv2.waitKey()
"""

# 抠图
"""
img = cv2.imread("puke.png")
width,height = 250,300
pts1 = np.float32([[445,30],[510,80],[366,124],[435,175]])
pts2 = np.float32([[0,0],[width,0],[0,height],[width,height]])
matrix = cv2.getPerspectiveTransform(pts1,pts2)
imgOut = cv2.warpPerspective(img,matrix,(width,height))
cv2.imshow("22",img)
cv2.imshow("变换后",imgOut)
cv2.waitKey()
"""

# 多张图像结合
"""
img = cv2.imread("5.png")
# 水平方向结合
imgHor = np.hstack((img,img))
# 竖直方向结合
imgVer = np.vstack((img,img))


cv2.imshow("Hor",imgHor)
cv2.imshow("Ver",imgVer)

cv2.waitKey()
"""

# 抓取颜色
"""
def empty(a):
    pass
# 创建窗体
cv2.namedWindow("TrackBars")
# 设置窗体大小
cv2.resizeWindow("TrackBars",640,240)
# 创建跟踪条
cv2.createTrackbar("Hua Min","TrackBars",0,179,empty)
cv2.createTrackbar("Hua Max","TrackBars",179,179,empty)
cv2.createTrackbar("Sat Min","TrackBars",0,255,empty)
cv2.createTrackbar("Sat Max","TrackBars",255,255,empty)
cv2.createTrackbar("Val Min","TrackBars",0,255,empty)
cv2.createTrackbar("Val Max","TrackBars",255,255,empty)


while True:
    # 将图像进行HSV处理
    img = cv2.imread("5.png")
    hsv = cv2.cvtColor(img,cv2.COLOR_BGR2HSV)
    # 设置滤镜最高值和最小值
    h_min = cv2.getTrackbarPos("Hua Min","TrackBars")
    h_max = cv2.getTrackbarPos("Hua Max","TrackBars")
    s_min = cv2.getTrackbarPos("Sat Min","TrackBars")
    s_max = cv2.getTrackbarPos("Sat Max","TrackBars")
    v_min = cv2.getTrackbarPos("Val Min","TrackBars")
    v_max = cv2.getTrackbarPos("Val Max","TrackBars")
    # 设置滤镜滤除
    # 0,16,0,255,153,255
    lower = np.array([h_min,s_min,v_min])
    upper = np.array([h_max,s_max,v_max])
    # 蒙版
    mask = cv2.inRange(hsv,lower,upper)
    # 叠加后的效果
    dst = cv2.bitwise_and(img, img, mask=mask)

    cv2.imshow("mask",mask)
    v1 = np.hstack((img,dst))
    cv2.imshow("all",v1)
    cv2.waitKey(1)
"""

# 形状检测
"""
def getContours(img):
    contours,hierarchy = cv2.findContours(img,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
    for cnt in contours:
        area = cv2.contourArea(cnt)
        print(area)
        cv2.drawContours(imgContour,cnt,-1,(0,0,0),2)

img = cv2.imread("5.png")
imgContour = img.copy()
imgGray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
imgBlur = cv2.GaussianBlur(imgGray,(9,9),1)
imgCanny = cv2.Canny(imgBlur,40,40)
getContours(imgCanny)

cv2.imshow("orgin",img)
cv2.imshow("imgGray",imgGray)
cv2.imshow("imgBlur",imgBlur)
cv2.imshow("imgCanny",imgCanny)
cv2.imshow("imgContour",imgContour)

cv2.waitKey()
"""

# 人脸识别（图片）
"""
faceCascade = cv2.CascadeClassifier("haarcascade_frontalface_alt2.xml")
img = cv2.imread("people/2.png")
imgGray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
faces = faceCascade.detectMultiScale(imgGray,1.1,4)
width = 200
for(x,y,w,h) in faces:
    cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)
    cv2.imshow("imgOut", img)
cv2.waitKey()
"""

# 人脸识别（视频）
"""
faceCascade = cv2.CascadeClassifier("haarcascade_frontalface_alt2.xml") #人脸识别的训练集
width = 200
cap = cv2.VideoCapture(0)
# 设置Windows宽
cap.set(3, 640)
# 设置Windows高
cap.set(4, 480)
# 设置亮度
cap.set(10,300)
while True:
    # 监听每一帧的图像
    success, img = cap.read()
    imgGray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    faces = faceCascade.detectMultiScale(imgGray, 1.1, 4)
    for(x,y,w,h) in faces:
        cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)
    cv2.imshow("imgOut", img)
    # 获取“q”来关闭
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break
"""

# 人脸识别+肤色识别（RGB）
"""
def colorgo(img):
    def empty(a):
        pass

    # 创建窗体
    cv2.namedWindow("TrackBars")
    # 设置窗体大小
    cv2.resizeWindow("TrackBars", 640, 240)
    # 创建跟踪条
    cv2.createTrackbar("Hua Min", "TrackBars", 0, 179, empty) # 1
    cv2.createTrackbar("Hua Max", "TrackBars", 179, 179, empty) # 1
    cv2.createTrackbar("Sat Min", "TrackBars", 2, 255, empty) # 1
    cv2.createTrackbar("Sat Max", "TrackBars", 200, 255, empty) # 0
    cv2.createTrackbar("Val Min", "TrackBars", 130, 255, empty) # 1
    cv2.createTrackbar("Val Max", "TrackBars", 254, 255, empty) # 1

    while True:
        # 将图像进行HSV处理
        hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
        # 设置滤镜最高值和最小值
        h_min = cv2.getTrackbarPos("Hua Min", "TrackBars")
        h_max = cv2.getTrackbarPos("Hua Max", "TrackBars")
        s_min = cv2.getTrackbarPos("Sat Min", "TrackBars")
        s_max = cv2.getTrackbarPos("Sat Max", "TrackBars")
        v_min = cv2.getTrackbarPos("Val Min", "TrackBars")
        v_max = cv2.getTrackbarPos("Val Max", "TrackBars")
        # 设置滤镜滤除
        # 0,16,0,255,153,255
        lower = np.array([h_min, s_min, v_min])
        upper = np.array([h_max, s_max, v_max])
        # 蒙版
        mask = cv2.inRange(hsv, lower, upper)
        # 叠加后的效果
        dst = cv2.bitwise_and(img, img, mask=mask)

        cv2.imshow("mask", mask)
        v1 = np.hstack((img, dst))
        cv2.imshow("all", v1)
        cv2.waitKey(1)

faceCascade = cv2.CascadeClassifier("haarcascade_frontalface_alt2.xml")
img = cv2.imread("people/2.png")
imgGray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
faces = faceCascade.detectMultiScale(imgGray,1.1,4)
width = 200
for(x,y,w,h) in faces:
    cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)
    # ******************************提取人脸部分，并且进行颜色识别，肤色
    pts1 = np.float32([[x, y], [x + w, y], [x, y + h], [x+w, y + h]])
    height = int(h/w * width)
    pts2 = np.float32([[0, 0], [width, 0], [0, height], [width, height]])
    matrix = cv2.getPerspectiveTransform(pts1, pts2)
    imgOut = cv2.warpPerspective(img, matrix, (width, height))
    colorgo(imgOut)
    # ******************************提取人脸部分，并且进行颜色识别，肤色
    cv2.imshow("imgOut", img)
cv2.waitKey()
"""
